package com.ceshiren.hogwarts.restassured.wechat.apiobject;

import com.ceshiren.hogwarts.restassured.wechat.utils.FakerUtils;
import io.restassured.http.ContentType;
import io.restassured.response.Response;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static io.restassured.RestAssured.given;

/**
 * @author Seven
 * @date 2020/12/25 14:47
 */
public class MembersObject {

    public static Response addMember(String userId,String name,String mobile,List<Integer> dIds,String accessToken){
        Map<String,Object> body = new HashMap<>();
        body.put("userid",userId+Thread.currentThread().getId());
        body.put("name",name+Thread.currentThread().getId());
        body.put("mobile",mobile);
        body.put("department",dIds);
        Response response = given().log().all()
                .contentType(ContentType.JSON)
                .queryParam("access_token",accessToken)
                .body(body)
                .when()
                .post("https://qyapi.weixin.qq.com/cgi-bin/user/create")
                .then().log().all().extract().response();
        return response;
    }

    public static String getUserId(String accessToken){
        String userId = "liuyi";
        String name = "liuyi";
        String mobile = FakerUtils.getTel();
        List<Integer> dIds = new ArrayList<>();
        dIds.add(1);
        dIds.add(2);
        Response response = addMember(userId,name,mobile,dIds,accessToken);
        String userid = response.path("userid").toString();
        return userid;
    }

    public static Response listMembers(String accessToken,String department_id){
        Response response = given().log().all()
                .contentType(ContentType.JSON)
                .queryParam("access_token",accessToken)
                .queryParam("department_id",department_id)
                .when()
                .get("https://qyapi.weixin.qq.com/cgi-bin/user/simplelist")
                .then().log().all().extract().response();
        return response;
    }

    public static Response delMembers(String accessToken,HashMap<String,List<String>> userId) {
        Response response = given().log().all()
                .contentType(ContentType.JSON)
                .queryParam("access_token",accessToken)
                .body(userId)
                .when()
                .post("https://qyapi.weixin.qq.com/cgi-bin/user/batchdelete")
                .then().log().all().extract().response();
        return response;
    }
}
